var printOrderMixin = function (templateName) {
  var typeObj = {
    "inoutWarehouseOrder": 1,
    "purchaseOrder": 2,
    "outBusinessOrder": 3,
    "issueOrder": 4
  };
  var type = typeObj[templateName];

  return {
    data: function () {
      return {
        printTempl: undefined,
        openPrintDialog: false
      };
    },

    methods: {
      toPreviewTemplateOrder: function (callback,printType) {
        var printTempl = this.printTempl;
        if (printTempl !== undefined) {
          this.openPrintDialog = true;
          if (!SysUtil.isEmpty(callback)) {
            callback(printTempl);
          }
          return;
        }

        if (type === 1) {
          if (printType === '入库') {
            type = 6;
          }
        }

        this.loading = true;
        var self = this;
        ajaxRequest('printTemplate/getPrintDataTemplateByType', {
          type: type
        }).then(function (templs) {
          self.loading = false;
          var templ = templs[0];
          var printTempl;
          if (templ === undefined) {
            printTempl = null;
          } else {
            printTempl = templ.template;
            if (SysUtil.isEmpty(printTempl)) {
              printTempl = null;
            }
          }
          self.printTempl = printTempl;
          if (!SysUtil.isEmpty(callback)) {
            callback(printTempl);
          }
          self.openPrintDialog = true;
        }).catch(function () {
          self.loading = false;
        });
      }
    }
  };
};

var printOrderTemplateMixin = function (templateName) {
  return {
    props: {
      printData: {
        type: Array,
        required: true
      },
      template: {
        type: String,
        required: true
      }
    },

    data: function () {
      var printTemplate = this.template;
      printTemplate = JSON.parse(printTemplate);
      printTemplate = this.formatTemplate(printTemplate);
      return {
        searchBtns: [],
        searchData: [],
        printTemplData: null,
        orderTemplate: printTemplate,
        printTemplate: null,
        readOnly: false
      };
    },

    created: function () {
      var orderTemplate = this.orderTemplate;
      var filterFields = orderTemplate.filterFields;
      if (SysUtil.isEmpty(filterFields)) {
        this.searchData = this.printData;
        this.setPrintData();
        this.resetOrderTemplate();
        return;
      }
      this.setSearchBtns(filterFields);
    },

    methods: {
      formatTemplate: function (printTemplate) {
        return printTemplate;
      },

      toPrint: function () {
        this.readOnly = true;
        var self = this;
        this.$nextTick(function () {
          var templateTable = self.$refs.printTemplateOrder;
          var $print = $(templateTable.$el).find('.print-area');
          $print = $print.clone();
          $print.css('display', 'none');
          $('#app').append($print);
          window.print();
          $print.remove();
          self.readOnly = false;
        });
      },

      setPrintData: function () {},

      setSearchBtns: function (filterFields) {
        var searchBtns = [];
        var printData = this.printData;
        var resetTemplSearchBtn = undefined;
        for (var i = 0; i < filterFields.length; i++) {
          var filterField = filterFields[i];
          var field = filterField.field;
          var obj = {
            field: field,
            type: 'select',
            label: filterField.label
          };

          var type = filterField.type;
          if (type === 'option') {
            var dropOption = this.getDropdownOption(obj, printData);
            if (dropOption === undefined || dropOption === null) {
              continue;
            }
            obj.options = dropOption.options;
            obj.value = dropOption.value;
          } else {
            var optionData = _.map(printData, field);
            optionData = _.uniq(optionData);
            obj.options = optionData;
            obj.value = optionData[0];
          }
          searchBtns.push(obj);
          if (field === this.resetTemplField) {
            resetTemplSearchBtn = obj;
          }
        }
        this.searchBtns = searchBtns;
        this.resetOrderTemplate(resetTemplSearchBtn);
        this.searchTemplData();
      },

      getDropdownOption: function () {
        return null;
      },

      searchChange: function () {
        this.searchTemplData();
      },

      searchTemplData: function () {
        var searchBtns = this.searchBtns;
        var printData = this.printData;
        var filterData = {};
        for (var i = 0; i < searchBtns.length; i++) {
          var searchBtn = searchBtns[i];
          if (searchBtn.nonSearch) {
            continue;
          }
          var type = searchBtn.type,
            value = searchBtn.value;
          if (type === 'select') {
            if (value !== '') {
              _.set(filterData, searchBtn.field, value);
            }
          }
        }
        printData = _.filter(printData, filterData);
        this.searchData = printData;
        this.setPrintData();
      },

      resetOrderTemplate: function () {
        this.printTemplate = this.orderTemplate;
      }
    },

    components: {
      SysPrintTemplateOrder: SysPrintTemplateOrder
    }
  };
};